<template>
  <div>
    <div>
      <el-row type="flex" :gutter="0" style="padding-left: 20px">
        <el-col :span="4">
          <h2 class="subtitle" style="margin-top: 15px; margin-left: 0px">
            <el-button type="text" @click="goback"><i class="el-icon-back subtitle"></i></el-button>新增员工风采
          </h2>
        </el-col>
        <el-col :span="20">
          <div style="padding: 20px 33px 0px 0px;float: right">
            <el-button class="el-green-button" @click="submitNew(formLabelAlign)"><i class="el-icon-search"></i>保存</el-button>
            <el-button class="el-red-button" @click="goback"><i class="el-icon-plus"></i>&nbsp;取消</el-button>
          </div>
        </el-col>
      </el-row>
<!--      <el-row type="flex" :gutter="20" style="padding-left: 20px;justify-content: space-between">-->
<!--        <span style="display: flex">-->
<!--        <el-button type="text" @click="goback"><i class="el-icon-back" style="color: white;font-size: xx-large"></i></el-button>-->
<!--        <h2 style="margin-top: 23px; margin-left: 5px">新增员工风采</h2>-->
<!--        </span>-->
<!--        <el-button style="height: 36px;margin: auto 5px;margin-right: 30px" class="el-blue-button" type="primary" @click="submitNew(formLabelAlign)">提交</el-button>-->
<!--      </el-row>-->
      <hr style="border-bottom: none;border-color: #8c939d"/>
      <div class="content-div">
        <el-form :label-position="'right'" label-width="80px" :model="formLabelAlign">
          <el-form-item label-width="0px">
            <el-col :span="15">
              <el-form-item label="标题" required>
                <el-input v-model="formLabelAlign.title"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="line" :span="2"></el-col>
            <el-col :span="8">
              <el-form-item label="时间" required>
                <el-date-picker type="date" placeholder="选择日期" v-model="formLabelAlign.timestamp" style="width: 100%;"></el-date-picker>
              </el-form-item>
            </el-col>
          </el-form-item>
          <el-form-item label="内容" required>
            <el-input type="textarea" v-model="formLabelAlign.content" :autosize="{ minRows:9, maxRows:20}"></el-input>
          </el-form-item>
          <el-form-item label="附件">
            <el-upload
              class="upload-demo"
              action="https://jsonplaceholder.typicode.com/posts/"
              :on-change="handleChange"
              :file-list="fileList"
              :auto-upload="false"
              list-type="picture">
              <el-button class="el-blue-button" size="small" type="primary">点击上传</el-button>
            </el-upload>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import request from '@/network/request'

export default {
  name: 'addDemeanor',
  data () {
    return {
      formLabelAlign: {
        id: -1,
        title: '',
        timestamp: '',
        content: ''
      },
      fileList: []
    }
  },
  methods: {
    handleRemove (file) {
    },
    handleChange (file) {
      this.fileList.push(file)
      console.log(this.fileList)
    },
    goback: function () {
      console.log('huitui')
      this.$router.back(-1)
    },
    submitNew (form) {
      const fd = new FormData()
      fd.append('title', form.title)
      fd.append('content', form.content)
      fd.append('timestamp', this.formatDate(new Date(form.timestamp), 'yyyy-MM-dd hh:mm:ss'))
      // 日志功能
      fd.append('randomID', this.$store.state.user.userId)
      for (var i = 0, len = this.fileList.length; i < len; i++) {
        fd.append('files', this.fileList[i].raw)
      }
      request({
        method: 'post',
        url: '/bulletin/addstaffmoment',
        data: fd
      }).then(res => {
        this.$message({
          message: '新建成功！',
          type: 'success'
        })
        this.goback()
        // eslint-disable-next-line handle-callback-err
      }, error => {
        this.$message.error('新建失败')
      })
    },
    formatDate (date, fmt) {
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      const o = {
        'M+': date.getMonth() + 1,
        'd+': date.getDate(),
        'h+': date.getHours(),
        'm+': date.getMinutes(),
        's+': date.getSeconds()
      }
      for (const k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
          const str = o[k] + ''
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str))
        }
      }
      return fmt
    },
    padLeftZero (str) {
      return ('00' + str).substr(str.length)
    }
  }
}
</script>

<style scoped>
.content-div {
  margin: 20px auto;
  width: 80%;
}
.el-form-item /deep/ textarea {
  background-color: transparent;
}
.el-form /deep/ label {
  color: white;
}
.el-form /deep/ .el-input__inner {
  color: white;
}
.el-form /deep/ .el-textarea__inner {
  color: white;
  width: 95%;
  min-height: 100px;
}
.el-form /deep/ div {
  color: white;
}
.el-form /deep/ .el-upload-list__item-name {
  color: white;
}
.el-form /deep/ .el-upload--picture-card {
  background-color: transparent;
}
</style>
